import React, { Component } from 'react';
import PropTypes from 'prop-types';
import './index.css';

export default class Footer extends Component {
  static propTypes = {
    checkAllTodo: PropTypes.func.isRequired,
    clearAllDone: PropTypes.func.isRequired
  };

  handleCheckAll = e => {
    const done = e.target.checked;
    this.props.checkAllTodo(done);
  };

  handleClearAllDone = () => {
    if (window.confirm('您确定删除已完成的事务吗？')) {
      this.props.clearAllDone();
    }
  };

  render() {
    const { todos } = this.props;
    const count = todos.reduce((sum, todo) => {
      return todo.done ? (sum = sum + 1) : sum;
    }, 0);
    const total = todos.length;
    return (
      <div className="todo-footer">
        <label>
          <input type="checkbox" name="" id="" checked={total && count === total} onChange={this.handleCheckAll} />
          已完成:{count} / 全部: {total}
        </label>
        <button className="btn btn-danger" onClick={this.handleClearAllDone}>
          清除已完成任务
        </button>
      </div>
    );
  }
}
